﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Text;

namespace MvcToolbox
{
    public class FeatureToggleSection : ConfigurationSection
    {
        [ConfigurationProperty("Database")]
        public DatabaseElement Database
        {
            get
            {
                return (DatabaseElement)this["Database"];
            }
            set
            { this["Database"] = value; }
        }

        [ConfigurationProperty("Features")]
        public FeaturesElement Features
        {
            get
            {
                return (FeaturesElement)this["Features"];
            }
            set
            { this["Features"] = value; }
        }
    }

    public class DatabaseElement : ConfigurationElement
    {
    }

    public class FeaturesElement : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FeatureElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FeatureElement)element).Name;
        }


        public FeatureElement this[int index]
        {
            get { return (FeatureElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }  
    }

    public class FeatureElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("isActive", IsRequired = false)]
        public bool? IsActive
        {
            get { return (bool?)base["isActive"]; }
            set { base["isActive"] = value; }
        }

        [ConfigurationProperty("activateAfterDate", IsRequired = false)]
        public string ActivateAfterDate
        {
            get { return (string)base["activateAfterDate"]; }
            set { base["activateAfterDate"] = value; }
        }

        [ConfigurationProperty("desactivateAfterDate", IsRequired = false)]
        public string DesactivateAfterDate
        {
            get { return (string)base["desactivateAfterDate"]; }
            set { base["desactivateAfterDate"] = value; }
        }

        

        [ConfigurationProperty("baseUrl", IsRequired = false)]
        public string BaseUrl
        {
            get { return (string)base["baseUrl"]; }
            set { base["baseUrl"] = value; }
        }
    }
}
